import re
import urllib.request


class PageHandler:
    savepath="C:\\Users\\wangzehao\\Desktop\\temp\\"
    def handle(self,page):
        imgurls=self.geturllist(page)
        for imgurl in imgurls:
            self.saveImg(imgurl,self.savepath+imgurl)
        return []
    
    def getImg(self,page):  
        reg = r'src="(.+?\.jpg)" pic_ext'  
        imgre = re.compile(reg)  
        return imgre.findall(page)
    
    def getLinkedPageUrls(self,page):
        return re.findall(r"(?<=href=\").+?(?=\")|(?<=href=\').+?(?=\')" ,page)
        
    def saveImg(self,imageURL,fileName):
        u = urllib.request.urlopen(imageURL)
        data = u.read()
        f = open(fileName, 'wb')
        f.write(data)
        f.close()